package csv;

import org.apache.commons.beanutils.BeanUtils;

import java.io.*;
import java.util.*;

/**
 * Description
 *
 * @ClassName csv
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/6/22
 * @Modified by none
 * @Modified Date
 */

public class CSV2 {

    /**
     * 生成为CVS文件
     *
     * @param exportData 源数据List
     * @param headMap csv文件的列表头map
     * @param outPutPath 文件路径
     * @param fileName 文件名称
     * @return
     */
    public static File createCSVFile(List<LinkedHashMap<String, String>> exportData, LinkedHashMap<String, String> headMap, String outPutPath, String fileName) {
        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            File file = new File(outPutPath + fileName + ".csv");
            if (!file.exists()) {
                boolean createNewFile = file.createNewFile();
                if (createNewFile) {
                    csvFile = file;
                }
            }
            // UTF-8使正确读取分隔符","
            csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GBK"), 1024);

            // 文件列表头
            if (headMap != null) {
                for (Iterator<Map.Entry<String, String>> it = headMap.entrySet().iterator(); it.hasNext();) {
                    Map.Entry<String, String> propertyEntry = it.next();
                    csvFileOutputStream.write('"' + propertyEntry.getValue() != null ? propertyEntry.getValue() : "" + '"');
                    if (it.hasNext()) {
                        csvFileOutputStream.write(",");
                    }
                }
                csvFileOutputStream.newLine();
            }

            // 写入文件内容
            for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
                Map row = (Map)iterator.next();
                for (Iterator<Map.Entry<String, String>> it = row.entrySet().iterator(); it.hasNext();) {
                    Map.Entry<String, String> propertyEntry = it.next();
                    csvFileOutputStream.write(BeanUtils.getProperty(row, propertyEntry.getKey()));
                    if (it.hasNext()) {
                        csvFileOutputStream.write(",");
                    }
                }
                if (iterator.hasNext()) {
                    csvFileOutputStream.newLine();
                }
            }
            csvFileOutputStream.flush();
        } catch (Exception e) {
            csvFile = null;

        } finally {
            try {
                if (csvFileOutputStream != null) {
                    csvFileOutputStream.close();
                }
            } catch (IOException e) {
                csvFile = null;
            }
        }
        return csvFile;
    }

    public static void main(String[] args) {
        //标题
        LinkedHashMap<String, String> headMap = new LinkedHashMap<String, String>();
        headMap.put("1","姓名");
        headMap.put("2","年龄");
        headMap.put("3","成绩");
        //内容
        List<LinkedHashMap<String, String>> exportData = new ArrayList<LinkedHashMap<String, String>>();
        LinkedHashMap<String, String> map1 = new LinkedHashMap<String, String>();
        map1.put("1","杨鲁华");
        map1.put("2","24");
        map1.put("3","100");
        exportData.add(map1);
        LinkedHashMap<String, String> map2 = new LinkedHashMap<String, String>();
        map2.put("1","陆婵");
        map2.put("2","23");
        map2.put("3","100");
        exportData.add(map2);
        CSV2.createCSVFile(exportData,headMap,"E:/","test");
    }
}
